package leetcode;

public class _0025 {
    /**
     * K 个一组翻转链表
     */
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            int n =0;
            ListNode t = head;
            while(t!=null){
                n++;
                t = t.next;
            }
            int num = 0;
            ListNode dummp = new ListNode(0,head);
            ListNode p0 = dummp;
            for(;n>=k;n -=k){
                ListNode pre = null;
                ListNode cur = p0.next;
                for(int i=0;i<k;i++){
                    ListNode nxt = cur.next;
                    cur.next= pre;
                    pre = cur;
                    cur = nxt;
                }
                ListNode nxt = p0.next;
                p0.next.next = cur;
                p0.next =  pre;
                p0 = nxt;
            }
            return dummp.next;
        }
        private class ListNode {
            ListNode next;
            int val;
            ListNode(){}
            ListNode(int val){
                this.val=val;
            }
            ListNode(int val, ListNode next){
                this.val = val;
                this.next = next;
            }
        }
    }
}
